package com.xinzhitong.www.service.impl;


import com.xinzhitong.www.exception.*;
import com.xinzhitong.www.exception.credentialcheckedrequire.CredentialNoCheckedException;
import com.xinzhitong.www.exception.credentialenroll.CredentialAlreadyExistsException;
import com.xinzhitong.www.exception.credentialenroll.CredentialEnrollErrException;
import com.xinzhitong.www.exception.credentialenroll.CredentialTimeIdErrException;
import com.xinzhitong.www.exception.credentialgeneral.NoCredentialGeneralException;
import com.xinzhitong.www.exception.credentialintroduction.NoIntroductionException;
import com.xinzhitong.www.exception.credentiallocation.CredentialNoLocationException;
import com.xinzhitong.www.exception.credentialpicture.CredentialNoPictureException;
import com.xinzhitong.www.exception.credentialrequest.NoRequestException;
import com.xinzhitong.www.exception.credentialtime.CredentialNoTimeException;
import com.xinzhitong.www.mapper.CredentialMapper;
import com.xinzhitong.www.pojo.ExamLocation;
import com.xinzhitong.www.pojo.ExamTime;
import com.xinzhitong.www.pojo.User;
import com.xinzhitong.www.service.CredentialService;
import com.xinzhitong.www.utils.FileHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 证书模块实现类
 *
 * @author 钟伟良
 */
@Service
@Transactional
public class CredentialServiceImpl implements CredentialService {

    @Resource
    private CredentialMapper credentialMapper;

    @Override
    public String introduction(Integer id) throws NoIntroductionException {
        String x = credentialMapper.selectIntroduction(id);
        if (x == null) {
            throw new NoIntroductionException();
        } else
            return x;
    }

    @Override
    public List<String> request(Integer credentialId) throws NoRequestException {
        List<String> x = credentialMapper.selectRequest(credentialId);
        if (x.size() == 0) {
            throw new NoRequestException();
        } else
            return x;
    }

    @Override
    public List<ExamLocation> location(Integer credentialId) throws CredentialLocationException {
        List<ExamLocation> x = credentialMapper.selectExamLocation(credentialId);
        if (x.size() == 0)
            throw new CredentialNoLocationException();
        else
            return x;

    }

    @Override
    public List<ExamTime> time(Integer locationId) throws CredentialTimeException {
        List<ExamTime> x = credentialMapper.selectTime(locationId);
        if (x.size() == 0)
            throw new CredentialNoTimeException();
        else
            return x;

    }

    @Override
    public List<String> checked(Integer credentialId) throws CredentialCheckedRequireException {
        List<String> x = credentialMapper.selectCheckRequired(credentialId);
        if (x.size() == 0) {
            throw new CredentialNoCheckedException();
        } else
            return x;
    }

    @Override
    public String picture(Integer id) throws CredentialPictureException {
        String x = credentialMapper.selectPicture(id);
        if (x == null)
            throw new CredentialNoPictureException();
        else
            return FileHelper.fileURL + x;
    }

    @Override
    public List<Map<String, Object>> general(Integer professionId) throws CredentialGeneralException {
        List<Map<String, Object>> x = credentialMapper.selectGeneral(professionId);
        if (x.size() == 0)
            throw new NoCredentialGeneralException();
        for (Map<String, Object> tmp : x) {
            tmp.put("picture", FileHelper.fileURL + tmp.get("picture"));
        }
        return x;

    }

    @Override
    public void examTime(Integer timeId) throws CredentialEnrollException {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        String x = user.getUsername();
        Integer z = credentialMapper.selectTimeIdExam(timeId);
        if (z == null)
            throw new CredentialTimeIdErrException();
        Integer s = credentialMapper.selectTimeId(x, timeId);
        if (s.equals(timeId))
            throw new CredentialAlreadyExistsException();
        Integer y = credentialMapper.insertExam(x, timeId);
        if (y != 1) throw new CredentialEnrollErrException();
    }
}
